<template>
    <div class="xx-flex-column">
        <div >
            <el-card v-if="userInfo.phone!='未登录'" class="box-card">
                <div class="xx-flex-row">
                    <div class="authorAvatar">

                    </div>
                    <div style="margin-left: 10px;">
                        <div class="xx-flex-row" style="    align-items: baseline;">
                            <h1>{{userInfo.name}}</h1>
                            <el-tag v-if="userInfo.identity!=-1" size="mini" style="margin-left:10px;" :type="getIdentityColor" effect="dark">{{getIdentityString}}</el-tag>
                        </div>
                        <p>您好，感谢您使用CodeShare，您将见证陪伴他的长大。</p>

                    </div>

                </div>
                <el-divider></el-divider>
                <p style="color: brown;">警告：最近更新项目管理和旧单页网页合并，新测试版本运行中，可能会出现BUG。请联系xswork@qq.com。</p>
                <p style="color: brown;">警告：完整项目的新建和目录树的使用可能存在一些逻辑问题和BUG，请联系xswork@qq.com。</p>
                <el-divider></el-divider>
                <div class="xx-flex-row item">
                    <div>姓名</div>
                    <div v-if="!editMode">{{userInfo.name}}</div>
                    <div style="width:100%;margin-left:10px;" v-else><el-input v-model="userInfo.name" placeholder="请输入姓名"></el-input></div>
                </div>
                <div class="xx-flex-row item">
                    <div>邮箱</div>
                    <div v-if="!editMode">{{userInfo.email}}</div>
                    <div style="width:100%;margin-left:10px;" v-else><el-input v-model="userInfo.email" placeholder="请输入邮箱"></el-input></div>
                </div>
                <div class="xx-flex-row item">
                    <div>手机</div>
                    <div v-if="!editMode">{{userInfo.phone}}</div>
                    <div style="width:100%;margin-left:10px;" v-else><el-input v-model="userInfo.phone" placeholder="请输入手机"></el-input></div>
                </div>
                <div class="xx-flex-row item">
                    <div>性别</div>
                    <div v-if="!editMode">{{userInfo.sex}}</div>
                    <div style="width:100%;margin-left:10px;" v-else><el-input v-model="userInfo.sex" placeholder="请输入性别"></el-input></div>
                </div>
                <div class="xx-flex-row item">
                    <div>签名</div>
                    <div v-if="!editMode">{{userInfo.sign}}</div>
                    <div style="width:100%;margin-left:10px;" v-else><el-input v-model="userInfo.sign" placeholder="请输入签名"></el-input></div>
                </div>
                <div class="xx-flex-row item">
                    <div>用户组</div>
                    <div v-if="userInfo.type==0">普通用户</div>
                    <div v-if="userInfo.type==1">管理员</div>
                </div>
                <div style="margin:20px;">
                    <el-button v-if="!editMode" @click="editInfo" type="primary" >修改个人资料</el-button>
                    <el-button v-if="editMode"  @click="saveInfo" type="success" >保存个人资料</el-button>
                    <el-button v-if="editMode"  @click="editInfo" type="danger" >取消</el-button>
                </div>

            </el-card>

        </div>

    </div>
</template>

<script>
import API from '../api/index'
export default {
    data() {
        return {
            userInfo: {
                phone: '未登录'
            },
            editMode: false
        }
    },
    computed: {
        getIdentityString() {
            switch (this.userInfo.identity) {
            case 0:
                return 'CodeShare管理员'
            case 1:
                return 'CodeShare金牌认证作者'
            case 2:
                return '初级实习作者'
            case 3:
                return '来源于网络收集'
            default:
                return ''
            }
        },
        getIdentityColor() {
            switch (this.userInfo.identity) {
            case 0:
                return ''
            case 1:
                return 'danger'
            case 2:
                return 'info'
            case 3:
                return ''
            default:
                return ''
            }
        }
    },
    activated() {
        this.$root.$emit('switchCollapse', false)
    },
    mounted() {
        this.showUserInfo()
        this.$root.$on('showUserInfo', () => { // 监听事件，这里$root很重要，不懂去官网介绍
            this.showUserInfo()
        })
    },
    methods: {
        saveInfo() {
            this.axios(API.user.updateUser(this.userInfo)).then(() => {
                this.$message({
                    message: '您的信息更改成功！',
                    type: 'success'
                })
                this.editMode = false
            }).catch((e) => {
                this.$message.error(e)
            })
        },
        editInfo() {
            this.editMode = !this.editMode
        },
        showUserInfo() {
            setTimeout(() => {
                this.userInfo = this.$store.state.userInfo
                console.log(this.$store.state.userInfo)
            }, 500)
        }
    }
}
</script>

<style scoped>
    .el-card{
        color: var(--text-color)  !important;
        background: var(--card-bg-color) !important;
        border: 0px !important;
    }
    .xx-flex-row{
        display: flex;
        flex-direction: row;
    }
    .item{
        margin:20px;
        font-size: 15px;
        justify-content: space-between;
    }
    .item>div{
        align-items: center;
        display: flex;
        word-break: keep-all;
    }
    .authorAvatar {
        background-image: url(https://assets.codepen.io/27503/internal/avatars/users/default.png?fit=crop&format=auto&height=80&version=1571410370&width=80);
        height: 50px;
        width: 50px;
        border-radius: 10px;
        background-position: center center;
        flex-shrink: 0;
    }
    h1{
        padding: 0;
        margin: 0;
    }
    p{
        padding: 0;
        margin: 0;
    }
    .box-card{
        text-align: left;
        width:100% !important;
        margin-top: 10px;
    }
    .xx-flex-column{
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;

    }
    .xx-flex-column >div{
        max-width: 1000px;
        width: 100%;
    }
    @media screen and (max-width: 600px) {
        .xx-flex-column {
            padding: 10px;
        }
        .xx-flex-column div{
            max-width: 100vh;
        }
    }
</style>
